package com.bjpowernode.manager;

import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.mapper.TClueRemarkMapper;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.model.TClue;
import com.bjpowernode.model.TCustomer;
import com.bjpowernode.query.CustomerQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.CustomerService;
import com.bjpowernode.util.JWTUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class CustomerManager {

    @Resource
    TCustomerMapper tCustomerMapper;

    @Resource
    TClueMapper tClueMapper;
    @Transactional(rollbackFor = Exception.class)
    public Boolean convertCustomer(CustomerQuery customerQuery) {
        //1、查询一下该线索是否已经转过客户，转过了，就不能再转
        TClue tClue = tClueMapper.selectByPrimaryKey(customerQuery.getClueId());
        if (tClue.getState() == -1) {
            throw new RuntimeException("该线索已经转换客户，不能再转了");
        }
        //2、没有转过，向客户表插入一条数据
        TCustomer tCustomer = new TCustomer();
        BeanUtils.copyProperties(customerQuery, tCustomer);
        //设置一下创建人
        Integer loginUserId = JWTUtil.parseUserFromJWT(customerQuery.getJwt()).getId();

        tCustomer.setCreateBy(loginUserId);

        int save = tCustomerMapper.insertSelective(tCustomer);
        //3、更新线索表的数据状态为-1
        TClue clue = new TClue();
        clue.setId(customerQuery.getClueId());
        clue.setState(-1); //-1表示已转客户
        int update = tClueMapper.updateByPrimaryKeySelective(clue);
        return save>=1&&update>=1;
    }
}
